package com.szyt.basicframe.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.szyt.basicframe.annotation.OperationLog;
import com.szyt.basicframe.annotation.RequiresPermissions;
import com.szyt.basicframe.common.ResponseResult;
import com.szyt.basicframe.common.StatusEnum;
import com.szyt.basicframe.pojo.request.usermanagement.CreateUserRequest;
import com.szyt.basicframe.pojo.request.usermanagement.SearchUserRequest;
import com.szyt.basicframe.pojo.request.usermanagement.UpdateUserRequest;
import com.szyt.basicframe.pojo.vo.UserDetailsVO;
import com.szyt.basicframe.pojo.vo.UserListItemVO;
import com.szyt.basicframe.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("user")
@Api(tags = "用户接口控制器", description = "用户控制器")
@Validated
public class UserController {

    private final ISysUserService userService;

    public UserController(ISysUserService userService) {
        this.userService = userService;
    }

    @RequiresPermissions("user:create")
    @ApiOperation("新增用户")
    @PostMapping("/create")
    @OperationLog(module = "用户管理",type = "POST",desc = "新增用户接口")
    public ResponseResult createUser(@RequestBody @Valid CreateUserRequest request) {
        boolean result = userService.createUser(request);
        return result ? ResponseResult.success() : ResponseResult.error(StatusEnum.DATA_INSERT_FAILED);
    }

    @RequiresPermissions("user:view")
    @ApiOperation("获取用户列表")
    @PostMapping("/list")
    @OperationLog(module = "用户管理",type = "POST",desc = "获取用户列表接口")
    public ResponseResult<IPage<UserListItemVO>> getUserList(@RequestBody SearchUserRequest request) {
        IPage<UserListItemVO> userlist = userService.searchUsers(request);
        return ResponseResult.success(userlist);
    }

    @RequiresPermissions("user:view")
    @ApiOperation("获取用户详情")
    @GetMapping("/details")
    @OperationLog(module = "用户管理", type = "GET", desc = "获取用户详情接口")
    public ResponseResult<UserDetailsVO> getUserDetails(@RequestParam Long id) {
        UserDetailsVO result = userService.getUserDetails(id);
        return ResponseResult.success(result);
    }

    @RequiresPermissions("user:update")
    @ApiOperation("更新用户信息")
    @PostMapping("/update")
    public ResponseResult updateUser(@RequestBody @Valid UpdateUserRequest request) {
        boolean exist = userService.existUserName(request.getId(), request.getUserName());
        if (exist) return ResponseResult.error(StatusEnum.VALIDATE_FAILED.getCode(), "用户名已存在");

        boolean result = userService.updateUser(request);
        return result ? ResponseResult.success() : ResponseResult.error(StatusEnum.DATA_UPDATE_FAILED);
    }

    @RequiresPermissions("user:update")
    @ApiOperation(("禁用用户"))
    @GetMapping("/disable/{userId}")
    public ResponseResult disableUser(@PathVariable Long userId) {
        boolean result = userService.disableUser(userId);
        return result ? ResponseResult.success() : ResponseResult.error(StatusEnum.DATA_UPDATE_FAILED);
    }

    @RequiresPermissions("user:update")
    @ApiOperation("启用用户")
    @GetMapping("/enable/{userId}")
    public ResponseResult enableUser(@PathVariable Long userId) {
        boolean result = userService.enableUser(userId);
        return result ? ResponseResult.success() : ResponseResult.error(StatusEnum.DATA_UPDATE_FAILED);
    }

    @RequiresPermissions("user:delete")
    @ApiOperation("删除用户")
    @DeleteMapping("/{userId}")
    public ResponseResult<Void> deleteUser(@PathVariable Long userId) {
        boolean result = userService.delete(userId);
        return result ? ResponseResult.success() : ResponseResult.error(StatusEnum.DATA_DELETE_FAILED);
    }
}
